package com.global.college.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Zhangzs
 * @create 2019/11/15 0015
 */
public class DateUtils {


    /**
     * 时间戳转换成时间
     */
    public static String stampToDate(long stamp, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);//这个是你要转成后的时间的格式
        Date date = new Date(stamp * 1000);
        String sd = sdf.format(date);
        return sd;
    }

    /**
     * 时间戳转化时间
     *
     * @param milSecond
     * @param pattern
     * @return
     */
    public static String getDateToString(String milSecond, String pattern) {
        if (pattern.equals("")) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        long lt = new Long(milSecond);
        return stampToDate(lt, pattern);
    }

    public static String getDownDate(String dateTime) {
        StringBuffer sb = new StringBuffer();
        long time = Long.parseLong(String.valueOf(dateTime));


        long minute = (long) Math.ceil(time / 60 / 1.0f);// 分钟前

        long hour = (long) Math.ceil(time / 60 / 60 / 1.0f);// 小时

        long day = (long) Math.ceil(time / 24 / 60 / 60 / 1.0f);// 天前

        long month = (long) Math.ceil(time / 30 / 24 / 60 / 60 / 1.0f);// 月前

        long year = (long) Math.ceil(time / 12 / 30 / 24 / 60 / 60 / 1.0f);// 年前

        if (month - 1 > 0) {
            sb.append(month + "个月");
        } else if (day - 1 > 0) {
            if (day >= 30) {
                sb.append("1个月");
            } else {
                sb.append(day + "天");
            }
        } else if (hour - 1 > 0) {
            if (hour >= 24) {
                sb.append("1天");
            } else {
                sb.append(hour + "个小时");
            }
        } else {
            if (minute == 60) {
                sb.append("1个小时");
            } else {
                sb.append(minute + "分钟");
            }
        }
        if (month <= 12) {
            sb.append("后");
        }

        return sb.toString();
    }

    /**
     * 计算时间差
     */
    public static String getStandardDate(String dateTime) {

        StringBuffer sb = new StringBuffer();

        long t = Long.parseLong(String.valueOf(dateTime));
        long time = System.currentTimeMillis() - (t * 1000);
        long mill = (long) Math.ceil(time / 1000);//秒前

        long minute = (long) Math.ceil(time / 60 / 1000.0f);// 分钟前

        long hour = (long) Math.ceil(time / 60 / 60 / 1000.0f);// 小时

        long day = (long) Math.ceil(time / 24 / 60 / 60 / 1000.0f);// 天前

        long month = (long) Math.ceil(time / 30 / 24 / 60 / 60 / 1000.0f);// 月前

        long year = (long) Math.ceil(time / 12 / 30 / 24 / 60 / 60 / 1000.0f);// 年前

        Calendar cal = Calendar.getInstance();

        long showYear = cal.get(Calendar.YEAR);

        long showMonth = cal.get(Calendar.MONTH);

        long showDay = cal.get(Calendar.DATE);

        long showHour = cal.get(Calendar.HOUR_OF_DAY);

        long showMint = cal.get(Calendar.MINUTE);

        if (month - 1 > 0) {
            if (month >= 12) {
                sb.append(showYear + "." + showMonth + "." + showDay + " " + showHour + ":" + showMint);
            } else {
                sb.append(month + "个月");
            }
        } else if (day - 1 > 0) {
            if (day >= 30) {
                sb.append("1个月");
            } else {
                sb.append(day + "天");
            }
        } else if (hour - 1 > 0) {
            if (hour >= 24) {
                sb.append("1天");
            } else {
                sb.append(hour + "个小时");
            }
        } else if (minute - 1 > 0) {
            if (minute == 60) {
                sb.append("1个小时");
            } else {
                sb.append(minute + "分钟");
            }
        } else if (mill - 1 > 0) {
            if (mill == 60) {
                sb.append("1分钟");
            } else {
                sb.append(mill + "秒");
            }
        } else {
            sb.append("刚刚");
        }
        if (month <= 12 && !sb.toString().equals("刚刚")) {
            sb.append("前");
        }
        return sb.toString();
    }
}
